package com.example.demo.controller;

import com.example.demo.model.Material;
import com.example.demo.model.vo.MaterialAddVo;
import com.example.demo.service.SpringBootService;
import com.example.demo.util.Result;
import com.example.demo.util.ResultVo;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by Administrator on 2018/12/26.
 */
@RestController
public class SpringBootController {

    @Autowired
    private SpringBootService springBootService;
    @GetMapping("/test/6666/yyyyyyyyyyyy")
    public String test(){
        return "666";
    }
    @GetMapping("/queryAll")
    private List<Material> queryAll(){
        return springBootService.queryAll();

    }
    @PostMapping("/batchQueryMaterials")
    private List<Material> batchQueryMaterials(@RequestBody List<Integer> ids){
        return springBootService.batchQueryMaterials(ids);

    }
    @PostMapping("/batchAddMaterials")
    private List<Material> batchAddMaterials(@RequestBody List<Material> materials){
        return springBootService.batchAddMaterials(materials);

    }

    @PostMapping("/batchDeleteMaterialsByIds")
    private String batchDeleteMaterialsByIds(@RequestBody List<Integer> ids){
        springBootService.batchDeleteMaterialsByIds(ids);
        return "success";

    }
    @PostMapping("/batchUpdateMaterialByIds")
    private List<Material> batchUpdateMaterialByIds(@RequestBody List<Material> materials){
        springBootService.batchUpdateMaterialByIds(materials);
        return materials;

    }

    @PostMapping("/zhuJieBatchQueryMaterials")
    private List<Material> SpringBootMapper(@RequestBody List<Integer> ids){
        return springBootService.zhuJieBatchQueryMaterials(ids);

    }

    @PostMapping("/alidData")
    private Result<MaterialAddVo> alidData(@RequestBody @Valid MaterialAddVo materialAddVo,BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            return ResultVo.error("500",bindingResult.getFieldError().getDefaultMessage());
        }

        return ResultVo.success(materialAddVo);

    }


}
